package com.wenjiang.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wenjiang.common.util.JWTUtil;
import com.wenjiang.entity.OperationLog;
import com.wenjiang.mapper.OperationLogMapper;
import org.apache.ibatis.annotations.Mapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;

/**
 * 操作日志的切面
 */
@Component
@Aspect
public class OperationLogAspect {

   @Autowired
   OperationLogMapper operationLogMapper;


   //重写环绕通知
    @Around(value = "@annotation(com.wenjiang.common.annotation.OperatorLog)")
    public  Object around (ProceedingJoinPoint JoinPoint) throws Throwable {


        //新建一个实体类
        OperationLog operationLog = new OperationLog();

        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //获取方法名
        MethodSignature signature = (MethodSignature)JoinPoint.getSignature();
        Method method = signature.getMethod();
        // 设置方法名
        operationLog.setOperateMethod(method.getName());
        // 设置起始时间
        Date now = new Date();
        operationLog.setOperateTime(simpleDateFormat.format(now));


        //获取方法的参数名
        Object[] args = JoinPoint.getArgs();
        //设置方法参数名 方法类型
        operationLog.setParamAndValue(Arrays.toString(args));
        operationLog.setOperateClass(JoinPoint.getTarget().getClass().getName());
        operationLog.setReturnClass(method.getReturnType().toString());
        //获取token 查询当前用户
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

        String token = request.getHeader("token");

        //设置用户名称
        HttpSession session = request.getSession();
        Object userName = session.getAttribute("userName");
        operationLog.setOperateUser(userName.toString());

        //开始执行
        Long startTime = System.currentTimeMillis();

        //执行方法
        Object proceed = JoinPoint.proceed();

        //结束时间
        Long endTime = System.currentTimeMillis();
        //设置耗时时间
        operationLog.setCostTime(endTime-startTime);
        operationLog.setReturnValue(proceed.toString());

        //插入数据
        operationLogMapper.insert(operationLog);

        return  proceed;


    }



}
